package com.c39.otc.open.api.dept;

import com.c39.cloud.rbac.facade.DepFacade;
import com.c39.cloud.rbac.model.dep.RbacDepDTO;
import com.c39.cloud.rbac.model.dep.RbacDepVO;
import com.c39.otc.open.api.dept.request.DeptProvinceRequest;
import com.c39.otc.open.api.dept.response.DeptProvinceResponse;
import com.cqfan.cloud.framework.base.PagedResult;
import com.cqfan.cloud.framework.base.Result;
import com.google.common.base.Splitter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门模块
 * @author JiaLing.FanX
 * @version 1.0
 * @description: TODO
 * @date 2025/9/10 16:50
 */


@RestController
@RequestMapping("v1/dept")
public class DeptProvinceController {


    @Resource
    private DepFacade depFacade;

    /**
     * 获取部门省份映射表
     * @return
     * @throws Exception
     */
    @GetMapping("/getDeptProvinces/")
    public Result<List<DeptProvinceResponse>> getDeptProvinces() throws Exception {
        RbacDepDTO dept = new RbacDepDTO();
        List<DeptProvinceResponse> deptProvinceResponses = new ArrayList<>();

        PagedResult<List<RbacDepVO>> result = depFacade.list(dept);
        List<RbacDepVO> rbacDepVOS = (List<RbacDepVO>) result.getData();
        for(RbacDepVO rbacDepVO : rbacDepVOS) {
            DeptProvinceResponse deptProvinceResponse = new DeptProvinceResponse();
            deptProvinceResponse.setDeptCode(rbacDepVO.getNumber());
            deptProvinceResponse.setDeptName(rbacDepVO.getName());
            if(!StringUtils.isEmpty(rbacDepVO.getProvinceName())) {
                List<String> provinces = Splitter.on(',')
                        .trimResults()
                        .omitEmptyStrings()
                        .splitToList(rbacDepVO.getProvinceName());

                deptProvinceResponse.setProvinces(provinces);
            }
            deptProvinceResponses.add(deptProvinceResponse);
        }
        return Result.success(deptProvinceResponses);
    }

}
